<?php
/**
 * View current, past and future contests
 *
 * $Id: contests.php 3209 2010-06-12 00:13:43Z eldering $
 *
 * Part of the DOMjudge Programming Contest Jury System and licenced
 * under the GNU GPL. See README and COPYING for details.
 */

require('init.php');
$title = 'Contests';
require(LIBWWWDIR . '/header.php');

echo "<h1>Contests</h1>\n\n";

if ( isset($_POST['unfreeze']) ) {
	$docid = array_pop(array_keys($_POST['unfreeze']));
	if ( $docid != $cid ) {
		error("Can only unfreeze for current contest");
	}
	$DB->q('UPDATE contest SET unfreezetime = %s WHERE cid = %i', now(), $docid);
}
// Get data. Starttime seems most logical sort criterion.
$res = $DB->q('TABLE SELECT * FROM contest WHERE post = 1 ORDER BY starttime DESC' );
if( count($res) == 0 ){
	echo "<p class=\"nodata\">No Postmaratones defined</p>\n\n";
}else{	
	$post_end = array();
	$now = now();
	for( $i=0; $i<count($res); $i++ ){
		if( difftime($now,$res[$i]['endtime']) > 0 ){
			$post_end[]=$res[$i];
		}
	}
	if( count($post_end) == 0 ){
		echo "No Postmaratones finish Yed";
	}else{
		echo "<form action=\"problem_post.php\" method=\"post\">\n".
		"<table class=\"list sortable\">\n<thead>\n" .
	     	"<tr><th scope=\"col\">CID</th><th scope=\"col\">active</th>" .
	     	"<th scope=\"col\">starts</th><th scope=\"col\">ends</th>" .
	     	"<th scope=\"col\">freeze<br />scores</th>" .
	     	"<th scope=\"col\">unfreeze<br />scores</th>" .
	    	 "<th scope=\"col\">name</th></tr>\n</thead>\n<tbody>\n";
		foreach( $post_end as $row){
			$link = '<a href="problem_post.php?id=' . urlencode($row['cid']).'">';
			echo '<tr class="' .( $iseven ? 'roweven': 'rowodd' ) .
			(!$row['enabled']    ? ' disabled' :'') .
			($row['cid'] == $cid ? ' highlight':'') . '">' .
			"<td align=\"right\">" . $link .
			"p" . (int)$row['cid'] . "</a></td>\n" .
			"<td title=\"".htmlspecialchars(@$row['activatetime']) . "\">" .
			$link . printtime($row['activatetime']) . "</a></td>\n" .
			"<td title=\"" . htmlspecialchars($row['starttime']) . "\">" .
			$link . printtime($row['starttime'])."</a></td>\n".
			"<td title=\"".htmlspecialchars($row['endtime']) . "\">" .
			$link . printtime($row['endtime'])."</a></td>\n".
			"<td title=\"".htmlspecialchars(@$row['freezetime']) . "\">" .
			$link . ( isset($row['freezetime']) ?
			printtime($row['freezetime']) : '-' ) . "</a></td>\n" .
			"<td title=\"".htmlspecialchars(@$row['unfreezetime']) . "\">" .
			$link . ( isset($row['unfreezetime']) ?
			printtime($row['unfreezetime']) : '-' ) . "</a></td>\n" .
			"<td>" . $link . htmlspecialchars($row['contestname']) . "</a></td>\n";
			$iseven = ! $iseven;
			echo "<td>";
			if ( $row['cid'] == $cid && isset($row['freezetime']) ) {
			echo "<input type=\"submit\" name=\"unfreeze[" . $row['cid'] .
			"]\" value=\"unfreeze scoreboard now\"" ;
			$n= now();
			if ( difftime($row['endtime'],$n) > 0 ||
				(isset($row['unfreezetime']) && difftime($row['unfreezetime'], $n) <= 0)){
				echo " disabled=\"disabled\"";
			}
			echo " />";
			}
			echo "</td>\n";
			echo "</tr>\n";	
		}
		echo "</tbody>\n</table>\n</form>\n\n";
	}
}
	


require(LIBWWWDIR . '/footer.php');
